package com.chance.cc.crawler.development.command.meta.keyword.pcauto;

import com.chance.cc.crawler.development.command.meta.keyword.CrawlerDomainKeywordCommand;
import com.chance.cc.crawler.meta.core.bean.crawler.CrawlerDomainKeys;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author lt
 * @version 1.0
 * @date 2021-01-13 14:26:12
 * @email okprog@sina.com
 */
public class PcAutoKeywords {

    private static final String domain = "pcauto";
    private static final String site = "series";
    private static CrawlerDomainKeywordCommand crawlerDomainKeywordCommand =
            new CrawlerDomainKeywordCommand("192.168.1.217", 9599);

    //日常采集车系id
    public static void main(String[] args) {
        String filepath = "E:\\chance_work\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\pcauto\\bmwkw.txt";
        List<String> content = getContent(filepath);
        System.out.println("去重前： " + content.size());
        Set<String> dupList = new HashSet<>();
        for (String keyword : content) {
            if(StringUtils.isBlank(keyword)){
                continue;
            }
//            keyword = keyword.split("cn/")[1].split("/")[0];
            System.out.println(keyword);
            dupList.add(keyword);
        }
        System.out.println("去重后： " + dupList.size());
        for (String keyword : dupList) {
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
        }
    }

    //宝马项目车系id
    @Test
    public void bmwTest(){
        String site = "bmwKw";
        crawlerDomainKeywordCommand = new CrawlerDomainKeywordCommand("192.168.1.217",9599);
        String filePath = "E:\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\pcauto\\bmwkw.txt";
        List<String> content = getContent(filePath);
        for (String keyword : content) {
            if(StringUtils.isBlank(keyword)){
                continue;
            }
            keyword = keyword.split("cn/")[1].replace("/", "");
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
           //crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
        }
    }

    //吉利项目车系id
    @Test
    public void geelyTest(){
        String site = "geely";
        crawlerDomainKeywordCommand = new CrawlerDomainKeywordCommand("192.168.1.217",9599);
        String filePath = "E:\\chance_work\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\pcauto\\jeely-kw.txt";
        List<String> content = getContent(filePath);
        for (String keyword : content) {
            if(StringUtils.isBlank(keyword)){
                continue;
            }
//            keyword = keyword.split("cn/")[1].replace("/", "");
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
//            crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
        }
    }

    //临时采集车系id
    @Test
    public void  adhocTest(){
        crawlerDomainKeywordCommand = new CrawlerDomainKeywordCommand("192.168.1.217",9599);
        String site = "adhocKw";
        String filePath = "E:\\chance_work\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\pcauto\\simple-kw.txt";
        List<String> content = getContent(filePath);
        for (String keyword : content) {
            if(StringUtils.isBlank(keyword)){
                continue;
            }
            keyword = keyword.split("cn/")[1].split("/")[0];
            System.out.println(keyword);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(keyword);
            crawlerDomainKeys.setStatus(0);
//            crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
            crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
        }
    }

    //通用项目车系id
    @Test
    public void  sgmTest(){
        crawlerDomainKeywordCommand = new CrawlerDomainKeywordCommand("192.168.1.217",9599);
        String site = "sgmKw";
        String filePath = "E:\\chance-crawler-development\\crawler-scripts-command\\src\\main\\java\\com\\chance\\cc\\crawler\\development\\command\\meta\\keyword\\pcauto\\sgmKw.txt";
        List<String> content = getContent(filePath);
        for (String keyword : content) {
            if (StringUtils.isBlank(keyword)){
                continue;
            }
            String[] split = keyword.split("https://price.pcauto.com.cn/");
            String kw = split[1].substring(0,split[1].length()-1);
            System.out.println(kw);
            CrawlerDomainKeys crawlerDomainKeys = new CrawlerDomainKeys();
            crawlerDomainKeys.setDomain(domain);
            crawlerDomainKeys.setSite(site);
            crawlerDomainKeys.setKeyword(kw);
            crawlerDomainKeys.setStatus(0);
           crawlerDomainKeywordCommand.addOrUpdateKeys(crawlerDomainKeys);
         //   crawlerDomainKeywordCommand.delKeys(crawlerDomainKeys);
        }
    }


    public static List<String> getContent(String filepath) {
        //读取文件
        List<String> lineLists = null;
        try {
            System.out.println(filepath);
            lineLists = Files
                    .lines(Paths.get(filepath), Charset.defaultCharset())
                    .flatMap(line -> Arrays.stream(line.split("\n")))
                    .collect(Collectors.toList());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return lineLists;
    }
}
